﻿using System.IO;
using Ionic.Zip;
using Ionic.Zlib;

namespace NetSurveillance.WebApp.Utilities
{
    public class ZipUtility
    {
        public static bool Compress(string[] filePaths, string savePath)
        {
            using (var zipFile = new ZipFile())
            {
                foreach (string filePath in filePaths)
                {
                    var dircPath = Path.GetFullPath(filePath);
                    zipFile.AddFile(filePath, dircPath);
                }
                zipFile.Save(savePath);
                return true;
            }
        }

        public static Stream Compress(string[] filePaths)
        {
            using (var zipFile = new ZipFile())
            {
                foreach (string filePath in filePaths)
                {
                    var dircPath = Path.GetFullPath(filePath);
                    zipFile.AddFile(filePath, dircPath);
                }
                using (var zipStream = new ZipOutputStream(new MemoryStream()))
                {
                    zipFile.Save(zipStream);
                    return zipStream;
                }
            }
        }

        public static byte[] Compress(string inputStreamName, byte[] inputStreamBytes, string[] filePaths)
        {
            using (var zipFile = new ZipFile())
            {
                zipFile.AddEntry(inputStreamName, inputStreamBytes);
                foreach (string filePath in filePaths)
                {
                    var dircPath = Path.GetFullPath(filePath);
                    zipFile.AddFile(filePath, dircPath);
                }
                using (var zipStream = new MemoryStream())
                {
                    zipFile.Save(zipStream);
                    return zipStream.ToArray();
                }
            }
        }
    }
}